package cn.gok.springsecuritydemo01.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * @author: chen fan
 * @since: 2022/11/18 10:22
 * @description:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("t_user")
public class User implements UserDetails {

    @TableId
    private Integer id;
    private String username;
    private String password;
    private Integer status;

    /**
     * 用户的权限集合
     */
    @TableField(exist = false)
    private Collection<? extends GrantedAuthority> authorities;

    /**
     * 账户是否未过期：  true 表示未过期
     */
    @TableField(exist = false)
    private boolean accountNonExpired = true;

    /**
     * 账户是否未锁定：  true 表示未锁定
     */
    @TableField(exist = false)
    private boolean accountNonLocked = true;

    /**
     * 密码是否未锁定：  true 表示未过期
     */
    @TableField(exist = false)
    private boolean credentialsNonExpired = true;

    /**
     * 账户是否启用，true表示启用
     */
    @TableField(exist = false)
    private boolean enabled = true;

    /**
     * 权限集合,一定不能为null值，否则会报错
     *     Spring Security会自动调用该方法获取权限列表信息
     * @return
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        return authorities;
    }



    @Override
    public boolean isAccountNonExpired() {
        return this.accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNonExpired;
    }

    /**
     * 用户是否启用，true表示启用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
}
